<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
  import echarts from 'echarts'

  require('echarts/theme/macarons') // echarts theme
  import {debounce} from '@/utils'

  export default {
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '700px'
      },
      chartData: {
        type: Object
      }
    },
    watch: {
      chartData(value) {
        this.initChart();
      }
    },
    data() {
      return {
        chart: null,
      }
    },
    mounted() {
      this.initChart()
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHandler)
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      window.removeEventListener('resize', this.__resizeHandler)
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
        this.chart.setOption({
          title: {
            text: '工序节拍(个/s)'
          },
          tooltip: {
            trigger: 'axis',
            formatter:function (params) {
              return params[0].data +"s"
            }
          },
          legend: {},
          toolbox: {
            show: true,
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: this.chartData.xAxis
          },
          yAxis: {
            name:"s",
            min:'dataMin', //取最小值为最小刻度
            max: 'dataMax', //取最大值为最大刻度
            type:'value',
            scale: true, //自适应
            // minInterval: 0.1, //分割刻度
            axisLabel: {
              // color: "#58f371",
              fontSize: 13,
              showMinLabel: false,//不显示最小刻度线值
              showMaxLabel: false ,//不显示最大刻度线值
              formatter:'{value} s'
            },
            axisLine: {
              lineStyle: {
                // color: "#58f371", //最左侧Y轴颜色
              }
            },
            splitLine: { //多条横线
              show: false
            }
          },
          series: [
            {
              type: 'line',
              data: this.chartData.data,
              smooth: false,
              markPoint: {
                data: [
                  // {type: 'max', name: 'Max'},
                  // {type: 'min', name: 'Min'}
                ]
              },
              itemStyle: {
                normal: {
                  color: "#1bdaf8", //折线点的颜色
                  lineStyle: {
                    color: "#0d427e" //折线的颜色
                  }
                }
              },
              markLine: {
                data: [

                ],
              }
            }
          ]
        })

      }
    }
  }
</script>
